from model import DicTemplate as dic
from Common import CommonHelper as cm
from DB import DBHelper as db
import json


class DicTemplateServices:
    __rows = []

    def __init__(self):
        print(cm.get_path('DB\studyDemo.db'))
        db_helper = db.DBHelper(cm.get_path('DB\studyDemo.db'))
        sql = 'select * from dic_template'
        self.__rows = db_helper.query(sql)
        # 关闭数据库连接
        db_helper.close()

    def get_dic_templates(self):
        dic_templates = []
        for row in self.__rows:
            dic_templates.append(dic.DicTemplate(row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
        return dic_templates

    def get_dic_templates_by_type_name(self, typename):
        dic_templates = self.get_dic_templates()
        group_dic_template = []
        for dic_template in dic_templates:
            if dic_template.dicType == typename:
                group_dic_template.append(dic_template)
        return group_dic_template

    def get_dic_templates_dict(self, typename):
        dic_templates = self.get_dic_templates()
        dicts = {}
        for dic_template in dic_templates:
            if dic_template.dicType == typename:
                dicts[dic_template.dicName] = dic_template.dicId
        return dicts

    def get_dic_templates_by_ids(self, dicids):
        dic_templates = self.get_dic_templates()
        result = []
        for dic_template in dic_templates:
            if dic_template.dicId in dicids:
                result.append(dic_template)
        return result



services = DicTemplateServices()
dicts = services.get_dic_templates()
dict_list = [obj.to_dict() for obj in dicts]
json_str = json.dumps(dict_list, ensure_ascii=False)
print(json_str)